//
//  lxAPPContainer.m
//  luodansc
//
//  Created by 倫 星 on 2012/10/21.
//  Copyright (c) 2012年 方正株式会社. All rights reserved.
//

#import "lxAPPContainer.h"
#import "lxGoodsEntity.h"

static lxAPPContainer * _lxAPPContainer;
@implementation lxAPPContainer
@synthesize userDefaults = _userDefaults;

+ (lxAPPContainer *)sharedLxAPPContainer {
    if (nil == _lxAPPContainer) {
		_lxAPPContainer = [[lxAPPContainer alloc] init];
        _lxAPPContainer.userDefaults = [NSUserDefaults standardUserDefaults];
	}
	return _lxAPPContainer;
}

- (id)init {
    self = [super init];
    if (nil != self) {
        _appInfoEntity = [[lxAppInfoEntity alloc] init];
        _userInfoEntity = [[lxUserInfoEntity alloc] init];
        _arrayShoppingList = [[NSMutableArray alloc]initWithCapacity:5];
        
        _hostReach = [[Reachability reachabilityWithHostName: test_site] retain];
        _internetReach = [[Reachability reachabilityForInternetConnection] retain];
        _wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
        
        _net_signal = NOTHING;
        
        [self reachabilityStart];
    }
    return self;
}

- (void)setUserInfoEntity:(lxUserInfoEntity *)userInfoEntity {
    _userInfoEntity.user_id = userInfoEntity.user_id;
    _userInfoEntity.email = userInfoEntity.email;
    _userInfoEntity.user_name = userInfoEntity.user_name;
    _userInfoEntity.password = userInfoEntity.password;
    _userInfoEntity.question = userInfoEntity.question;
    _userInfoEntity.answer = userInfoEntity.answer;
    _userInfoEntity.sex = userInfoEntity.sex;
    _userInfoEntity.birthday = userInfoEntity.birthday;
    _userInfoEntity.user_money = userInfoEntity.user_money;
    _userInfoEntity.frozen_money = userInfoEntity.frozen_money;
    _userInfoEntity.pay_points = userInfoEntity.pay_points;
    _userInfoEntity.rank_points = userInfoEntity.rank_points;
    _userInfoEntity.address_id = userInfoEntity.address_id;
    _userInfoEntity.reg_time = userInfoEntity.reg_time;
    _userInfoEntity.last_login = userInfoEntity.last_login;
    _userInfoEntity.last_time = userInfoEntity.last_time;
    _userInfoEntity.last_ip = userInfoEntity.last_ip;
    _userInfoEntity.visit_count = userInfoEntity.visit_count;
    _userInfoEntity.user_rank = userInfoEntity.user_rank;
    _userInfoEntity.is_special = userInfoEntity.is_special;
    _userInfoEntity.salt = userInfoEntity.salt;
    _userInfoEntity.parent_id = userInfoEntity.parent_id;
    _userInfoEntity.flag = userInfoEntity.flag;
    _userInfoEntity.alias = userInfoEntity.alias;
    _userInfoEntity.msn = userInfoEntity.msn;
    _userInfoEntity.qq = userInfoEntity.qq;
    _userInfoEntity.office_phone = userInfoEntity.office_phone;
    _userInfoEntity.home_phone = userInfoEntity.home_phone;
    _userInfoEntity.mobile_phone = userInfoEntity.mobile_phone;
    _userInfoEntity.is_validated = userInfoEntity.is_validated;
    _userInfoEntity.credit_line = userInfoEntity.credit_line;
    _userInfoEntity.passwd_question = userInfoEntity.passwd_question;
    _userInfoEntity.passwd_answer = userInfoEntity.passwd_answer;
    _userInfoEntity.user_bonus = userInfoEntity.user_bonus;
}

- (lxUserInfoEntity*)getUserInfoEntity {
    return _userInfoEntity;
}

- (void)delUserInfoEntity {
    lxUserInfoEntity *userInfoEntity = [[lxUserInfoEntity alloc] init];
    [self setUserInfoEntity:userInfoEntity];
}

- (void)setAppInfoEntity:(lxAppInfoEntity *)appInfoEntity {
    _appInfoEntity.name = appInfoEntity.name;
    _appInfoEntity.version = appInfoEntity.version;
}

- (lxAppInfoEntity *)getAppInfoEntity {
    return _appInfoEntity;
}

- (void)addShopping:(lxGoodsEntity*)goodsEntity {
    if (nil == goodsEntity) {
        return;
    }
    [_arrayShoppingList addObject:goodsEntity];
}

- (NSMutableArray*)getArrayShoppingList {
    int count = [_arrayShoppingList count];
    if (count > 0) {
        return _arrayShoppingList;
    }
    return nil;
}

//--------------------------------------------------
// ネットワーク接続状況確認
//--------------------------------------------------
- (BOOL) updateInterfaceWithReachability:(Reachability*)curReach
{
	// 接続状態を取得
	NetworkStatus status = [curReach currentReachabilityStatus];
	
    // ホスト接続
	if(curReach == _hostReach)
	{
		if( status == NotReachable )
		{
            _net_signal = HostConnNG;
			NSLog(@"Host connection is failed.");
            return NO;
		} else {
            _net_signal = HostConnOK;
			NSLog(@"Host connection is successful.");
            return YES;
		}
    }
    
	// 3Gネットワーク接続
	if(curReach == _internetReach)
	{
		if( status == NotReachable )
		{
            _net_signal = InternetConnNG;
			NSLog(@"3G network is failed.");
            return NO;
		} else {
            _net_signal = InternetConnOK;
			NSLog(@"3G network is successful.");
            return YES;
		}
	}
    
	// Wi-Fi接続
	if(curReach == _wifiReach)
	{
		if( status == NotReachable )
		{
            _net_signal = WifiConnNG;
			NSLog(@"Wi-Fi is failed.");
            return NO;
		} else {
            _net_signal = WifiConnOK;
			NSLog(@"Wi-Fi is successful.");
            return YES;
		}
	}
    return NO;
}

// ネットワーク接続状況確認
- (void)reachabilityStart
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged:)
                                                 name: kReachabilityChangedNotification
                                               object: nil];
    
    // ホスト接続を確認
    _hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
    [_hostReach startNotifier];
    [self updateInterfaceWithReachability: _hostReach];

    // 3G接続を確認
    _internetReach = [[Reachability reachabilityForInternetConnection] retain];
    [_internetReach startNotifier];
    [self updateInterfaceWithReachability: _internetReach];
    
    // Wi-Fi接続を確認
    _wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
    [_wifiReach startNotifier];
    [self updateInterfaceWithReachability: _wifiReach];
}

// ネットワーク接続の状態が変化したときに呼ばれる
- (void)reachabilityChanged:(NSNotification*)note
{
	Reachability* curReach = [note object];
	NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
	[self updateInterfaceWithReachability: curReach];
    
    switch (_net_signal) {
        case HostConnOK: {
/*
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", nil)
                                                            message:NSLocalizedString(@"network_host_changed", nil)
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"ok", nil)
                                                  otherButtonTitles:nil];
                [alert show];
                [alert release];
 */
                [_delegate netHostReachChangedOK];
            }
            break;
        case HostConnNG: {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"error", nil)
                                                            message:NSLocalizedString(@"network_conn_failed", nil)
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"ok", nil)
                                                  otherButtonTitles:nil];
                [alert show];
                [alert release];
                [_delegate netHostReachChangedNG];
            }
            break;
        case InternetConnOK: {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", nil)
                                                            message:NSLocalizedString(@"network_3g_changed", nil)
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"ok", nil)
                                                  otherButtonTitles:nil];
                [alert show];
                [alert release];
                [_delegate netInternetReachChangedOK];
            }
            break;
        case InternetConnNG: {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"error", nil)
                                                            message:NSLocalizedString(@"network_conn_failed", nil)
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"ok", nil)
                                                  otherButtonTitles:nil];
                [alert show];
                [alert release];
                [_delegate netInternetReachChangedNG];
            }
            break;
        case WifiConnOK: {
/*
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", nil)
                                                            message:NSLocalizedString(@"network_wifi_changed", nil)
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"ok", nil)
                                                  otherButtonTitles:nil];
                [alert show];
                [alert release];
 */
                [_delegate netWifiReachChangedOK];
            }
            break;
        case WifiConnNG: {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"error", nil)
                                                            message:NSLocalizedString(@"network_conn_failed", nil)
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"ok", nil)
                                                  otherButtonTitles:nil];
                [alert show];
                [alert release];
                [_delegate netWifiReachChangedNG];
            }
            break;
        default: {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"error", nil)
                                                            message:NSLocalizedString(@"network_conn_failed", nil)
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"ok", nil)
                                                  otherButtonTitles:nil];
                [alert show];
                [alert release];
            }
            break;
    }
}

- (BOOL)isExistNet {
    if (HostConnOK == _net_signal || InternetConnOK == _net_signal || WifiConnOK == _net_signal) {
        return YES;
    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"error", nil)
                                                    message:NSLocalizedString(@"network_conn_failed", nil)
                                                   delegate:self
                                          cancelButtonTitle:NSLocalizedString(@"ok", nil)
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    
    return NO;
}

- (BOOL)isIPhone {
    BOOL isPhone = NO;
    if(UI_USER_INTERFACE_IDIOM()) {
        isPhone = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone);
    }
    return isPhone;
}

- (void)dealloc {
    [_appInfoEntity release];
    [_userInfoEntity release];
    [_arrayShoppingList release];
    [_hostReach release];
    [_internetReach release];
    [_wifiReach release];
    _net_signal = NOTHING;
    [super dealloc];
}

@end
